package leetcode_500;

/**
 *@author 周杨
 *AddStrings_415 两个代表数字字符串相加
 *describe:按位加 AC 99%
 *2018年7月23日 下午2:31:09
 */
public class AddStrings_415 {
	
	public static void main(String[] args) {
		AddStrings_415 test=new AddStrings_415();
		System.out.println(test.addStrings("0", "0"));
	}
	
	public String addStrings(String num1, String num2) {
		if(num1.length()==0)
			return num2;
		if(num2.length()==0)
			return num1;
        char c1[]=num1.toCharArray();
        char c2[]=num2.toCharArray();
        boolean flag=false;
        char res[]=new char[Math.max(c1.length, c2.length)];
        int index=res.length-1;
        int index1=c1.length-1;
        int index2=c2.length-1;
        while(index1>=0||index2>=0) {
        	char now1,now2;
        	if(index1<0)
        		now1='0';
        	else
        		now1=c1[index1];
        	if(index2<0)
        		now2='0';
        	else
        		now2=c2[index2];
        	int now=now1+now2-2*'0'+(flag?1:0);
        	if(now>=10) {
        		now%=10;
        		flag=true;
        	}
        	else {
        		flag=false;
        	}
        	res[index]=(char)(now+48);
        	--index;
        	--index1;
        	--index2;
        }
        String r=new String(res);
        return flag?"1"+r:r;
    }
}
